% xcorr between different sessions

oo=figure;

plotit=1;
for site=1:size(proj_meta,2)
    no_time_points = size(proj_meta(site).rd,2);
    
    act=[];
    type_sessions={'f','p','d'};
    max_ac = [];
    for tp=1:no_time_points
        act=[];
        for lyr=1:size(proj_meta(site).rd,1)
            act=[act; proj_meta(site).rd(lyr,tp).act];
        end
        
        max_ac(tp,:) = max(act,[],2);
    end
    result_corr=[];
    for ind=1:no_time_points
        for jnd=1:no_time_points
            result_corr(ind,jnd)=diag(corrcoef(max_ac(ind,:),max_ac(jnd,:)),-1);
        end
    end
    figure(oo),subplot(ceil(size(proj_meta,2)/3),3,site);
    imagesc(result_corr)
    title([regexprep(proj_meta(site).animal,'\_','\\_') ' - ' num2str(proj_meta(site).ExpGroup(1))]);
    colorbar
    set(gca,'Clim',[0 1])
end

% xcorr btw fb and dark
oo=figure;
plotit=1;
for site=1:size(proj_meta,2)
    no_time_points = size(proj_meta(site).rd,2);
    
    act=[];
    type_sessions={'f','p','d'};
    max_ac = [];
    for tp=1:no_time_points
        act=[];
        for lyr=1:size(proj_meta(site).rd,1)
            act=[act; proj_meta(site).rd(lyr,tp).act];
        end
        sess_start=proj_meta(site).rd(lyr,tp).nbr_frames;
        sess_start=cumsum(sess_start);
        sess_start=[1 sess_start+1];
        ac_sess=find(strcmp(proj_meta(site).rd(lyr,tp).session,type_sessions(2))==1);
        indices=[];
        for kk=1:length(ac_sess)
            indices=[indices sess_start(ac_sess(kk)):sess_start(ac_sess(kk)+1)-1];
        end
        fb_max(tp,:)=max(act(:,indices),[],2);
        
        indices=[];
        sess_start=proj_meta(site).rd(lyr,tp).nbr_frames;
        sess_start=cumsum(sess_start);
        sess_start=[1 sess_start+1];
        ac_sess=find(strcmp(proj_meta(site).rd(lyr,tp).session,type_sessions(3))==1);
        indices=[];
        for kk=1:length(ac_sess)
            indices=[indices sess_start(ac_sess(kk)):sess_start(ac_sess(kk)+1)-1];
        end
        dark_max(tp,:)=max(act(:,indices),[],2);
        
        
        max_ac(tp,:) = max(act,[],2);
    end
    result_corr=[];
    for ind=1:no_time_points
        for jnd=1:no_time_points
            result_corr(ind,jnd)=diag(corrcoef(max_ac(ind,:),max_ac(jnd,:)),-1);
        end
    end
    figure(oo),subplot(ceil(size(proj_meta,2)/3),3,site);
    imagesc(result_corr)
    title([regexprep(proj_meta(site).animal,'\_','\\_') ' - ' num2str(proj_meta(site).ExpGroup(1))]);
    colorbar
    set(gca,'Clim',[0 1])
end

